// history-order-list.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        page_offset: 0,
        page_size: 10,
        is_load_all: false,
        showNoData: false,
        merchants: []
    },
    confirmReceive: function (event) {
        var id = event.currentTarget.dataset.id
        var confirm = event.currentTarget.dataset.confirm
        var that = this
        app.http.request({
            url: '/api/v1/trade/confirm_receive', //仅为示例，并非真实的接口地址
            method: 'POST',
            data: {
                id: id,
                confirm: confirm
            },
            dataType: 'json',
            success: function (res) {

                wx.showToast({
                    title: res.data.msg,
                    duration:2000
                })
            },
            fail: function () {

            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        app.http.request({
            url: '/api/v1/user/takeaway?offset=' + this.data.page_offset * this.data.page_size, //仅为示例，并非真实的接口地址
            method: 'GET',
            data: {},
            dataType: 'json',
            success: function (res) {
                console.log(res.data.data)
                if (res.data.code == '200') {
                    that.setData({
                        merchants: res.data.data.merchants,
                        showNoData: res.data.data.merchants.length <= 0
                    })
                }
            },
            fail: function () {

            }
        })
    },
    doRate: function (event) {
        var id = event.currentTarget.dataset.id
        app.http.request({
            url: '/api/v1/merchant/rate',
            data: {
                id: id
            },
            success: function (res) {

            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
    doPay: function (e) {

        var  order_sn = e.currentTarget.dataset.sn;

        getApp().http.request({
            url: '/api/v1/trade/request_pay',
            data: {
                sn: order_sn,
            },
            dataType: 'json',
            success: function (res) {
                console.log(res.data.code)
                console.log(res.data.data.wx)
                if (res.data.code == 500){
                    wx.showToast({
                        title: res.data.msg,
                        duration: 2000,
                    })
                }else{
                    wx.requestPayment({

                        'timeStamp': res.data.data.wx.timeStamp,
                        'nonceStr': res.data.data.wx.nonceStr,
                        'package': res.data.data.wx.package,
                        'signType': res.data.data.wx.signType,
                        'paySign': res.data.data.wx.paySign,
                        'success': function (res) {
                            wx.redirectTo({
                                url: '/pages/my-takeaway-list/my-takeaway-list?order_sn=' + order_sn
                            })
                        },
                        'fail': function (res) {
                        },
                        'complete': function (res) {
                        }
                    })
                }
            }
        })
    },
    openDetail: function (e) {
        var order_sn = e.currentTarget.dataset.sn;
        wx.navigateTo({
            url: '/pages/my-takeaway-detail/my-takeaway-detail?order_sn=' + order_sn,
        })
    },
    bindscrolltolower: function () {
        var offset = this.data.page_offset
        offset++
        this.setData({
            page_offset: offset
        })
        console.log(offset)
        var path = '/api/v1/user/takeaway?offset=' + this.data.page_offset * this.data.page_size
        var that = this
        getApp().http.request({
            url: path, //仅为示例，并非真实的接口地址
            method: 'GET',
            data: {},
            dataType: 'json',
            success: function (res) {
                console.log(res)
                if (res.data.data.list.length > 0) {
                    var tmp_listdata = that.data.merchants
                    for (var i = 0; i < res.data.data.merchants.length; i++) {
                        tmp_listdata.push(res.data.data.merchants[i])
                    }
                    that.setData({
                        categories: res.data.data.categories,
                        merchants: tmp_listdata
                    })
                } else {
                    if (!that.data.is_load_all) {
                        wx.showToast({
                            title: '没有更多数据了',
                            duration:2000
                        })
                        that.setData({
                            is_load_all: true
                        })
                    }
                }
            },
            fail: function () {

            }
        })
    }
})